<template>
	<view class="newsDetail">
		<view class="newsDetail_box">
			<!-- 上部 -->
			<view class="top">
				<!-- 标题 -->
				<view class="title">
					<text v-if="data.title === '' || data.title === null">暂无标题</text>
					<text v-else>{{data.title}}</text>
				</view>
				<view class="time">
					{{time}}
				</view>
				<view class="content" v-html="data.content != null ? data.content : '暂无内容'">
				</view>
			</view>
			<!-- 底部 -->
			<view class="bottom">
				<view class="hits">
					阅读 {{data.hits}}
				</view>
				<view class="share">
					<view class="friend" @click="shareFriend()">
						<image src="../../static/img/32.png" mode=""></image>
						<text>发送给朋友</text>
					</view>
					<view class="friends" @click="share()">
						<image src="../../static/img/31.png" mode=""></image>
						<text>转到朋友圈</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		Index
	} from "../../static/api/apis.js"
	export default {
		data() {
			return {
				id: "",
				data: {},
			}
		},
		onReady() {
			document.querySelector('.uni-page-head__title').style.fontWeight = 'normal'
		},

		computed: {
			time() {
				if (this.data.addtime === null || this.data.addtime === "") {
					return "暂无添加时间"
				} else {
					if (this.data.addtime) {
						return this.data.addtime.slice(this.data.addtime.indexOf("-") + 1, this.data.addtime.indexOf(":") -
							2).replace("-", " 月 ") + "日"
					} else {
						return this.data.addtime
					}
				}
			}
		},
		onLoad(e) {
			this.id = e.id
			this.getNewsDetail()
		},
		methods: {
			// 分享给朋友
			shareFriend() {
				uni.share({
					provider: "weixin",
					scene: "WXSceneSession",
					type: 1,
					summary: this.data.title,
					success: function(res) {
						console.log("success:" + JSON.stringify(res));
					},
				 fail: function(err) {
						console.log("fail:" + JSON.stringify(err));
					}
				});
			},
			// 分享到朋友圈
			share() {
				uni.share({
					provider: "weixin",
					scene: "WXSceneTimeline",
					type: 1,
					summary: this.data.title,
					success: function(res) {
						console.log("success:" + JSON.stringify(res));
					},
					fail: function(err) {
						console.log("fail:" + JSON.stringify(err));
					}
				});
			},
			// 获取资讯详情
			async getNewsDetail() {
				let res = await Index.newsGet({
					id: this.id
				})
				if (res.errCode === 10000) {
					this.data = res.data
					console.log('资讯详情', this.data)
				}
			},
			// 返回上一个页面
			back() {
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style lang="less" scoped>
	@import url(../../static/css/newsDetail.less);
</style>
